Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UseLambdaForFunctionalInterface recipe should not convert when code uses a static field from enum constructor #415

Conversation

jevanlingen
Copy link
Contributor

What's changed?

No more lambda conversion for UseLambdaForFunctionalInterface recipe when the anonymous class is within a constructor of a Enum class and uses a static field of this very class.

What's your motivation?

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

… uses a static field from enum constructor
… uses a static field from enum constructor
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/CovariantEquals.java
    • lines 80-80
  • src/main/java/org/openrewrite/staticanalysis/EmptyBlockVisitor.java
    • lines 215-216
  • src/main/java/org/openrewrite/staticanalysis/ExplicitInitializationVisitor.java
    • lines 72-72
    • lines 94-94
  • src/main/java/org/openrewrite/staticanalysis/FinalizeMethodArguments.java
    • lines 208-208
  • src/main/java/org/openrewrite/staticanalysis/HiddenFieldVisitor.java
    • lines 199-199
    • lines 244-244
  • src/main/java/org/openrewrite/staticanalysis/HideUtilityClassConstructorVisitor.java
    • lines 98-98
    • lines 128-128
    • lines 191-191
  • src/main/java/org/openrewrite/staticanalysis/NoRedundantJumpStatements.java
    • lines 98-98
  • src/main/java/org/openrewrite/staticanalysis/OperatorWrap.java
    • lines 96-96
    • lines 134-134
    • lines 182-182
    • lines 215-215
    • lines 246-246
    • lines 283-283
    • lines 316-316
    • lines 364-364
    • lines 397-397
  • src/main/java/org/openrewrite/staticanalysis/RenameMethodsNamedHashcodeEqualOrToString.java
    • lines 69-70
  • src/main/java/org/openrewrite/staticanalysis/ReplaceDuplicateStringLiterals.java
    • lines 249-249
  • src/main/java/org/openrewrite/staticanalysis/TernaryOperatorsShouldNotBeNested.java
    • lines 357-357
  • src/main/java/org/openrewrite/staticanalysis/UseLambdaForFunctionalInterface.java
    • lines 73-73
    • lines 117-117

…ot-convert-when-code-uses-a-static-field-from-enum-constructor
@timtebeek
Copy link
Contributor

Thanks for taking this on! From a reasoning and maintenance perspective I want to propose an alternative: not make any change within Enums.

            @Override
            public J visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
                if (classDecl.getKind() == J.ClassDeclaration.Kind.Type.Enum) {
                    return classDecl;
                }
                return super.visitClassDeclaration(classDecl, ctx);
            }

Given that we rarely expect any lambdas within enum classes, I think it's fair to then just ignore any such cases if they occur. It would greatly simplify the logic here, and cover 3 out of the 5 tests you've written here, with the other 2 ignored to likely little effect. Saves us from then having to reason about and maintain these conditionals for any such edge cases. Any thoughts on that?

@jevanlingen
Copy link
Contributor Author

jevanlingen commented Dec 27, 2024

Given that we rarely expect any lambdas within enum classes

Well actually: "Given that we rarely expect any lambdas anonymous classes convertible to lambda's within enum classes", which I think will be even less frequently used than lambda within enum classes. So I agree. As I was writing the logic, more and more edge cases started to appear. Eventually the logic became quite specific and indeed difficult to read.

So let's simplify it indeed. I will change the code and modify the test accordingly 😎.

@timtebeek timtebeek merged commit 237f8b8 into main Dec 27, 2024
0 of 2 checks passed
@timtebeek timtebeek deleted the 413-uselambdaforfunctionalinterface-should-not-convert-when-code-uses-a-static-field-from-enum-constructor branch December 27, 2024 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working recipe
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

UseLambdaForFunctionalInterface should not convert when code uses a static field from enum constructor
2 participants